<?php
namespace app\serverfavorite\logic;

use \app\manage\logic\BaseLogic;
use \app\manage\service\ApiBase;
use \think\Db;

/**
 * @author:      李颖
 * @DateTime:    2017-06-12 10:49:46
 * @Description: 用户收藏
 */
class Favorite extends BaseLogic
{
	protected $name = 'favorite';
    protected $config = 'db_config_favorite';
    protected $favoriteModel = 'app\serverfavorite\model\Favorite';
    protected $addAllowField = [];
    protected $searchWhere = ['user_id'=>'eq','cate_id'=>'eq','obj_id'=>'eq'];

    use \app\serversetting\traits\OperateModel;

    /*db获取某个收藏详情*/
    public function dbOneFavorite($map)
    {
    	$_where = $this->searchWhere($map, $this->searchWhere);
    	$_where['status'] = 1;
    	return $this->db()->where($_where)->find();
    }

    /*添加一个收藏*/
    public function addFavorite($data): array 
    {
        unset($data['app_id']);
    	$res = $this->model($this->favoriteModel)->save($data);
    	return auto_return($res, '添加成功', '添加失败');
    }

    /*取消一个收藏*/
    public function cancelOne($data): array 
    {
    	$_where['id'] = $data['id'];
        $_where['user_id'] = $data['user_id'];
    	$_where['status'] = 1;
    	$saveData['status'] = 0;
    	$res = $this->model($this->favoriteModel)->save($saveData, $_where);
    	return auto_return($res);
    }

    /*获取一个收藏详情*/
    public function favoriteInfo($data): array 
    {
        $_where['id'] = $data['id'];
    	$_where['user_id'] = $data['user_id'];
    	$_where['status'] = 1;
    	$res = $this->model($this->favoriteModel)->where($_where)->find();
    	return auto_return($res);
    }

    /*我的 - 获取收藏列表   字段名field_name  字段值field_value */
    public function userFavoriteList($data): array 
    {
    	$_where = $this->searchWhere($data, $this->searchWhere);
        if(isset($data['field_name']) && isset($data['field_value'])){  //对快照里的某个字段进行搜索
            $field = 'favorite.snapshoot$.' . $data['field_name'];
            $_where[$field] = $data['field_value'];
        }
        $_where['favorite.status'] = 1;

        //限制app_id
        $manageAppId = ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : 'favorite_category.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR favorite_category.is_public = 1';

    	$res = Db::connect(config('db_config_favorite'))
                ->view('favorite','*')
                ->view('favorite_category',['app_id','other_app_read','is_public'],'favorite.cate_id = favorite_category.id','LEFT')
                ->where($_where)
                ->where($_whereOr)
                ->order('favorite.create_time', 'desc')
                ->paginate(null,false,['page'=>$data['page'],'list_rows'=>$data['listRows']])
                ->toArray();
        if($res['total'] > 0){
            foreach ($res['data'] as $k => &$v) {
                isset($v['user_info']) && $v['user_info'] = json_decode($v['user_info'], true);
                isset($v['snapshoot']) && $v['snapshoot'] = json_decode($v['snapshoot'], true);
            }
        }
        return auto_return_page($res);
    }

    /*后台 - 获取收藏列表*/
    public function favoriteList($data): array 
    {
        //return $this->_getList($data, 'favorite', 'favorite_category', $this->model($this->favoriteModel), true, 'cate_id');
        $manageAppId = ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : 'fc.`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR fc.is_public = 1';
        $data['user_id'] && $_where['f.user_id'] = $data['user_id'];
        $_where['f.status'] = 1;
        $list = $this->model($this->favoriteModel)
                    ->alias('f')
                    ->field('f.*,fc.action as cate_action,fc.name as cate_name')
                    ->join('favorite_category fc', 'fc.id = f.cate_id', 'LEFT')
                    ->where($_where)
                    ->where($_whereOr)
                    ->order('f.id desc')
                    ->paginate(null,false,['page'=>$data['page'],'list_rows'=>$data['listRows']])
                    ->toArray();

            return auto_return_page($list);
    }
 



}